
<!DOCTYPE HTML>
<html lang="" >
    <head>
        <meta charset="UTF-8">
        <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
        <title>双向绑定v-model的工作原理 · petite-vue源码剖析</title>
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <meta name="description" content="">
        <meta name="generator" content="GitBook 3.2.3">
        <meta name="author" content="^_^肥仔John">
        
        
    
    <link rel="stylesheet" href="../../gitbook/style.css">

    
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-search-pro/search.css">
                
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-code/plugin.css">
                
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-back-to-top-button/plugin.css">
                
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-tbfed-pagefooter/footer.css">
                
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-chapter-fold/chapter-fold.css">
                
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-expandable-chapters/expandable-chapters.css">
                
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-splitter/splitter.css">
                
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-anchor-navigation-ex/style/plugin.css">
                
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-donate/plugin.css">
                
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-highlight/website.css">
                
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-fontsettings/website.css">
                
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-theme-comscore/test.css">
                
            
        

    

    
        
    
        
    
        
    
        
    
        
    
        
    

        
    
    
    <meta name="HandheldFriendly" content="true"/>
    <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <link rel="apple-touch-icon-precomposed" sizes="152x152" href="../../gitbook/images/apple-touch-icon-precomposed-152.png">
    <link rel="shortcut icon" href="../../gitbook/images/favicon.ico" type="image/x-icon">

    
    <link rel="next" href="4_ref.html" />
    
    
    <link rel="prev" href="2_event_binding.html" />
    

    </head>
    <body>
        
<div class="book">
    <div class="book-summary">
        
            
<div id="book-search-input" role="search">
    <input type="text" placeholder="Type to search" />
</div>

            
                <nav role="navigation">
                


<ul class="summary">
    
    

    

    
        
        <li class="header">前言</li>
        
        
    
        <li class="chapter " data-level="1.1" data-path="../../">
            
                <a href="../../">
            
                    
                    《Petite-Vue源码剖析》简介
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2" data-path="../../0_intro/1_reason_for_source_reading.html">
            
                <a href="../../0_intro/1_reason_for_source_reading.html">
            
                    
                    为什么要读源码
            
                </a>
            

            
        </li>
    

    
        
        <li class="header">在线渲染</li>
        
        
    
        <li class="chapter " data-level="2.1" data-path="../0_intro.html">
            
                <a href="../0_intro.html">
            
                    
                    章节介绍
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.2" data-path="../1_start_from_static_view.html">
            
                <a href="../1_start_from_static_view.html">
            
                    
                    从静态视图首次渲染开始
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.3" data-path="../2_initial_rendering_and_rerendering_for_v-if.html">
            
                <a href="../2_initial_rendering_and_rerendering_for_v-if.html">
            
                    
                    条件v-if的首次渲染和重新渲染
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.4" data-path="../3_initial_rendering_and_rerendering_for_v-for.html">
            
                <a href="../3_initial_rendering_and_rerendering_for_v-for.html">
            
                    
                    循环v-for的首次渲染和重新渲染
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.5" data-path="1_attribute_binding.html">
            
                <a href="1_attribute_binding.html">
            
                    
                    属性绑定v-bind的工作原理
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.6" data-path="2_event_binding.html">
            
                <a href="2_event_binding.html">
            
                    
                    事件绑定v-on的工作原理
            
                </a>
            

            
        </li>
    
        <li class="chapter active" data-level="2.7" data-path="3_bidirectional_binding.html">
            
                <a href="3_bidirectional_binding.html">
            
                    
                    双向绑定v-model的工作原理
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.8" data-path="4_ref.html">
            
                <a href="4_ref.html">
            
                    
                    特殊指令ref的工作原理
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.9" data-path="../5_tips_for_template.html">
            
                <a href="../5_tips_for_template.html">
            
                    
                    优化手段template的原理和使用技巧
            
                </a>
            

            
        </li>
    

    
        
        <li class="header">响应式系统</li>
        
        
    
        <li class="chapter " data-level="3.1" data-path="../../2_reactivity_system/0_intro.html">
            
                <a href="../../2_reactivity_system/0_intro.html">
            
                    
                    章节介绍
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.2" data-path="../../2_reactivity_system/1_reactive_object_array.html">
            
                <a href="../../2_reactivity_system/1_reactive_object_array.html">
            
                    
                    逐行解读reactive如何构建对象和数组的响应式对象
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.3" data-path="../../2_reactivity_system/2_reactive_map_set.html">
            
                <a href="../../2_reactivity_system/2_reactive_map_set.html">
            
                    
                    逐行解读reactive如何构建Map和Set的响应式对象
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.4" data-path="../../2_reactivity_system/3_register_side_effect.html">
            
                <a href="../../2_reactivity_system/3_register_side_effect.html">
            
                    
                    逐行解读effect如何注册副作用函数
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.5" data-path="../../2_reactivity_system/4_cleanup_algorithm.html">
            
                <a href="../../2_reactivity_system/4_cleanup_algorithm.html">
            
                    
                    依赖清理优化算法详解-算法思路
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.6" data-path="../../2_reactivity_system/5_smi.html">
            
                <a href="../../2_reactivity_system/5_smi.html">
            
                    
                    依赖清理优化算法详解-基于JavaScript引擎的SMI进一步压榨性能
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.7" data-path="../../2_reactivity_system/6_trigger_and_scheduler.html">
            
                <a href="../../2_reactivity_system/6_trigger_and_scheduler.html">
            
                    
                    逐行解读trigger和调度器
            
                </a>
            

            
        </li>
    

    
        
        <li class="header">沙箱模型</li>
        
        
    
        <li class="chapter " data-level="4.1" data-path="../../3_sandbox.html">
            
                <a href="../../3_sandbox.html">
            
                    
                    模板表达式的沙箱模型
            
                </a>
            

            
        </li>
    

    

    <li class="divider"></li>

    <li>
        <a href="https://www.gitbook.com" target="blank" class="gitbook-link">
            Published with GitBook
        </a>
    </li>
</ul>


                </nav>
            
        
    </div>

    <div class="book-body">
        
            <div class="body-inner">
                
                    

<div class="book-header" role="navigation">
    

    <!-- Title -->
    <h1>
        <i class="fa fa-circle-o-notch fa-spin"></i>
        <a href="../.." >双向绑定v-model的工作原理</a>
    </h1>
</div>




                    <div class="page-wrapper" tabindex="-1" role="main">
                        <div class="page-inner">
                            
<div id="book-search-results">
    <div class="search-noresults">
    
                                <section class="normal markdown-section">
                                
                                <div id="anchor-navigation-ex-navbar"><i class="fa fa-navicon"></i><ul><li><span class="title-icon "></span><a href="#&#x53CC;&#x5411;&#x7ED1;&#x5B9A;v-model&#x7684;&#x5DE5;&#x4F5C;&#x539F;&#x7406;"><b>1. </b>&#x53CC;&#x5411;&#x7ED1;&#x5B9A;v-model&#x7684;&#x5DE5;&#x4F5C;&#x539F;&#x7406;</a></li><ul><li><span class="title-icon "></span><a href="#&#x524D;&#x8A00;"><b>1.1. </b>&#x524D;&#x8A00;</a></li><li><span class="title-icon "></span><a href="#&#x6DF1;&#x5165;v-model&#x5DE5;&#x4F5C;&#x539F;&#x7406;"><b>1.2. </b>&#x6DF1;&#x5165;v-model&#x5DE5;&#x4F5C;&#x539F;&#x7406;</a></li><li><span class="title-icon "></span><a href="#compositionstart&#x548C;compositionend&#x662F;&#x4EC0;&#x4E48;&#xFF1F;"><b>1.3. </b>compositionstart&#x548C;compositionend&#x662F;&#x4EC0;&#x4E48;&#xFF1F;</a></li><li><span class="title-icon "></span><a href="#&#x7F16;&#x7801;&#x65B9;&#x5F0F;&#x89E6;&#x53D1;&#x4E8B;&#x4EF6;"><b>1.4. </b>&#x7F16;&#x7801;&#x65B9;&#x5F0F;&#x89E6;&#x53D1;&#x4E8B;&#x4EF6;</a></li><ul><li><span class="title-icon "></span><a href="#&#x521B;&#x5EFA;&#x548C;&#x521D;&#x59CB;&#x5316;&#x4E8B;&#x4EF6;&#x5BF9;&#x8C61;"><b>1.4.1. </b>&#x521B;&#x5EFA;&#x548C;&#x521D;&#x59CB;&#x5316;&#x4E8B;&#x4EF6;&#x5BF9;&#x8C61;</a></li><li><span class="title-icon "></span><a href="#&#x6DFB;&#x52A0;&#x76D1;&#x542C;&#x548C;&#x53D1;&#x5E03;&#x4E8B;&#x4EF6;"><b>1.4.2. </b>&#x6DFB;&#x52A0;&#x76D1;&#x542C;&#x548C;&#x53D1;&#x5E03;&#x4E8B;&#x4EF6;</a></li><li><span class="title-icon "></span><a href="#&#x9488;&#x5BF9;petite-vue&#x8FDB;&#x884C;&#x5206;&#x6790;"><b>1.4.3. </b>&#x9488;&#x5BF9;petite-vue&#x8FDB;&#x884C;&#x5206;&#x6790;</a></li><li><span class="title-icon "></span><a href="#&#x5916;&#x756A;&#xFF1A;ie&#x7684;&#x4E8B;&#x4EF6;&#x6A21;&#x62DF;"><b>1.4.4. </b>&#x5916;&#x756A;&#xFF1A;IE&#x7684;&#x4E8B;&#x4EF6;&#x6A21;&#x62DF;</a></li></ul></ul></ul></div><h1 id="&#x53CC;&#x5411;&#x7ED1;&#x5B9A;v-model&#x7684;&#x5DE5;&#x4F5C;&#x539F;&#x7406;"><a name="&#x53CC;&#x5411;&#x7ED1;&#x5B9A;v-model&#x7684;&#x5DE5;&#x4F5C;&#x539F;&#x7406;" class="anchor-navigation-ex-anchor" href="#&#x53CC;&#x5411;&#x7ED1;&#x5B9A;v-model&#x7684;&#x5DE5;&#x4F5C;&#x539F;&#x7406;"><i class="fa fa-link" aria-hidden="true"></i></a>1. &#x53CC;&#x5411;&#x7ED1;&#x5B9A;v-model&#x7684;&#x5DE5;&#x4F5C;&#x539F;&#x7406;</h1>
<h2 id="&#x524D;&#x8A00;"><a name="&#x524D;&#x8A00;" class="anchor-navigation-ex-anchor" href="#&#x524D;&#x8A00;"><i class="fa fa-link" aria-hidden="true"></i></a>1.1. &#x524D;&#x8A00;</h2>
<p>&#x53CC;&#x5411;&#x7ED1;&#x5B9A;<code>v-model</code>&#x4E0D;&#x4EC5;&#x4EC5;&#x662F;&#x5BF9;&#x53EF;&#x7F16;&#x8F91;HTML&#x5143;&#x7D20;(<code>select</code>, <code>input</code>, <code>textarea</code>&#x548C;&#x9644;&#x5E26;<code>[contenteditable=true]</code>)&#x540C;&#x65F6;&#x9644;&#x52A0;<code>v-bind</code>&#x548C;<code>v-on</code>&#xFF0C;&#x800C;&#x4E14;&#x8FD8;&#x80FD;&#x5229;&#x7528;&#x901A;&#x8FC7;petite-vue&#x9644;&#x52A0;&#x7ED9;&#x5143;&#x7D20;&#x7684;<code>_value</code>&#x3001;<code>_trueValue</code>&#x548C;<code>_falseValue</code>&#x5C5E;&#x6027;&#x63D0;&#x4F9B;&#x5B58;&#x50A8;&#x975E;&#x5B57;&#x7B26;&#x4E32;&#x503C;&#x7684;&#x80FD;&#x529B;&#x3002;</p>
<h2 id="&#x6DF1;&#x5165;v-model&#x5DE5;&#x4F5C;&#x539F;&#x7406;"><a name="&#x6DF1;&#x5165;v-model&#x5DE5;&#x4F5C;&#x539F;&#x7406;" class="anchor-navigation-ex-anchor" href="#&#x6DF1;&#x5165;v-model&#x5DE5;&#x4F5C;&#x539F;&#x7406;"><i class="fa fa-link" aria-hidden="true"></i></a>1.2. &#x6DF1;&#x5165;v-model&#x5DE5;&#x4F5C;&#x539F;&#x7406;</h2>
<pre><code class="lang-ts"><span class="hljs-keyword">export</span> <span class="hljs-keyword">const</span> model: Directive&lt;
  HTMLInputElement | HTMLSelectElement | HTMLTextAreaElement
&gt; = ({ el, exp, <span class="hljs-keyword">get</span>, effect, modifers }) =&gt; {
  <span class="hljs-keyword">const</span> <span class="hljs-keyword">type</span> = el.type
  <span class="hljs-comment">// &#x901A;&#x8FC7;`with`&#x5BF9;&#x4F5C;&#x7528;&#x57DF;&#x7684;&#x53D8;&#x91CF;/&#x5C5E;&#x6027;&#x8D4B;&#x503C;</span>
  <span class="hljs-keyword">const</span> assign = <span class="hljs-keyword">get</span>(<span class="hljs-string">`val =&gt; { <span class="hljs-subst">${exp}</span> = val }`</span>)
  <span class="hljs-comment">// &#x82E5;type&#x4E3A;number&#x5219;&#x9ED8;&#x8BA4;&#x5C06;&#x503C;&#x8F6C;&#x6362;&#x4E3A;&#x6570;&#x5B57;</span>
  <span class="hljs-keyword">const</span> { trim, <span class="hljs-built_in">number</span> = <span class="hljs-keyword">type</span> ==== <span class="hljs-string">&apos;number&apos;</span>} = modifiers || {}

  <span class="hljs-keyword">if</span> (el.tagName === <span class="hljs-string">&apos;select&apos;</span>) {
    <span class="hljs-keyword">const</span> sel = el as HTMLSelectElement
    <span class="hljs-comment">// &#x76D1;&#x542C;&#x63A7;&#x4EF6;&#x503C;&#x53D8;&#x5316;&#xFF0C;&#x66F4;&#x65B0;&#x72B6;&#x6001;&#x503C;</span>
    listen(el, <span class="hljs-string">&apos;change&apos;</span>, () =&gt; {
      <span class="hljs-keyword">const</span> selectedVal = <span class="hljs-built_in">Array</span>.prototype.filter
        .call(sel.options, (o: HTMLOptionElement) =&gt; o.selected)
        .map((o: HTMLOptionElement) =&gt; <span class="hljs-built_in">number</span> ? toNumber(getValue(o)) : getValue(o))
      assign(sel.multiple ? selectedVal : selectedVal[<span class="hljs-number">0</span>])
    })

    <span class="hljs-comment">// &#x76D1;&#x542C;&#x72B6;&#x6001;&#x503C;&#x53D8;&#x5316;&#xFF0C;&#x66F4;&#x65B0;&#x63A7;&#x4EF6;&#x503C;</span>
    effect(() =&gt; {
      value = <span class="hljs-keyword">get</span>()
      <span class="hljs-keyword">const</span> isMultiple = sel.muliple
      <span class="hljs-keyword">for</span> (<span class="hljs-keyword">let</span> i = <span class="hljs-number">0</span>, l = sel.options.length; i &lt; i; i++) {
        <span class="hljs-keyword">const</span> option = sel.options[i]
        <span class="hljs-keyword">const</span> optionValue = getValue(option)
        <span class="hljs-keyword">if</span> (isMulitple) {
          <span class="hljs-comment">// &#x5F53;&#x4E3A;&#x591A;&#x9009;&#x4E0B;&#x62C9;&#x6846;&#x65F6;&#xFF0C;&#x5165;&#x53C2;&#x8981;&#x4E48;&#x662F;&#x6570;&#x7EC4;&#xFF0C;&#x8981;&#x4E48;&#x662F;Map</span>
          <span class="hljs-keyword">if</span> (isArray(value)) {
            option.selected = looseIndexOf(value, optionValue) &gt; <span class="hljs-number">-1</span>
          }
          <span class="hljs-keyword">else</span> {
            option.selected = value.has(optionValue)
          }
        }
        <span class="hljs-keyword">else</span> {
          <span class="hljs-keyword">if</span> (looseEqual(optionValue, value)) {
            <span class="hljs-keyword">if</span> (sel.selectedIndex !== i) sel.selectedIndex = i
            <span class="hljs-keyword">return</span>
          }
        }
      }
    })
  }
  <span class="hljs-keyword">else</span> <span class="hljs-keyword">if</span> (<span class="hljs-keyword">type</span> === <span class="hljs-string">&apos;checkbox&apos;</span>) {
    <span class="hljs-comment">// &#x76D1;&#x542C;&#x63A7;&#x4EF6;&#x503C;&#x53D8;&#x5316;&#xFF0C;&#x66F4;&#x65B0;&#x72B6;&#x6001;&#x503C;</span>
    listen(el, <span class="hljs-string">&apos;change&apos;</span>, () =&gt; {
      <span class="hljs-keyword">const</span> modelValue = <span class="hljs-keyword">get</span>()
      <span class="hljs-keyword">const</span> checked = (el as HTMLInputElement).checked
      <span class="hljs-keyword">if</span> (isArray(modelValue)) {
        <span class="hljs-keyword">const</span> elementValue = getValue(el)
        <span class="hljs-keyword">const</span> index = looseIndexOf(modelValue, elementValue)
        <span class="hljs-keyword">const</span> found = index !== <span class="hljs-number">-1</span>
        <span class="hljs-keyword">if</span> (checked &amp;&amp; !found) {
          <span class="hljs-comment">// &#x52FE;&#x9009;&#x4E14;&#x4E4B;&#x524D;&#x6CA1;&#x6709;&#x88AB;&#x52FE;&#x9009;&#x8FC7;&#x7684;&#x5219;&#x52A0;&#x5165;&#x5230;&#x6570;&#x7EC4;&#x4E2D;</span>
          assign(modelValue.concat(elementValue))
        }
        <span class="hljs-keyword">else</span> <span class="hljs-keyword">if</span> (!checked &amp;&amp; found) {
          <span class="hljs-comment">// &#x6CA1;&#x6709;&#x52FE;&#x9009;&#x4E14;&#x4E4B;&#x524D;&#x5DF2;&#x52FE;&#x9009;&#x7684;&#x6392;&#x9664;&#x540E;&#x5728;&#x91CD;&#x65B0;&#x8D4B;&#x503C;&#x7ED9;&#x6570;&#x7EC4;</span>
          <span class="hljs-keyword">const</span> filered = [...modelValue]
          filteed.splice(index, <span class="hljs-number">1</span>)
          assign(filtered)
        }
        <span class="hljs-comment">// &#x5176;&#x5B83;&#x60C5;&#x51B5;&#x5C31;&#x5565;&#x90FD;&#x4E0D;&#x5E72;&#x54AF;</span>
      }
      <span class="hljs-keyword">else</span> {
        assign(getCheckboxValue(el as HTMLInputElement, checked))
      }
    })

    <span class="hljs-comment">// &#x76D1;&#x542C;&#x72B6;&#x6001;&#x503C;&#x53D8;&#x5316;&#xFF0C;&#x66F4;&#x65B0;&#x63A7;&#x4EF6;&#x503C;</span>
    <span class="hljs-keyword">let</span> oldValue: <span class="hljs-built_in">any</span>
    effect(() =&gt; {
      <span class="hljs-keyword">const</span> value = <span class="hljs-keyword">get</span>()
      <span class="hljs-keyword">if</span> (isArray(value)) {
        ;(el as HTMLInputElement).checked = 
          looseIndexOf(value, getValue(el)) &gt; <span class="hljs-number">-1</span>
      }
      <span class="hljs-keyword">else</span> <span class="hljs-keyword">if</span> (value !== oldValue) {
        ;(el as HTMLInputElement).checked = looseEqual(
          value,
          getCheckboxValue(el as HTMLInputElement, <span class="hljs-literal">true</span>)
        )
      }
      oldValue = value
    })
  }
  <span class="hljs-keyword">else</span> <span class="hljs-keyword">if</span> (<span class="hljs-keyword">type</span> === <span class="hljs-string">&apos;radio&apos;</span>) {
    <span class="hljs-comment">// &#x76D1;&#x542C;&#x63A7;&#x4EF6;&#x503C;&#x53D8;&#x5316;&#xFF0C;&#x66F4;&#x65B0;&#x72B6;&#x6001;&#x503C;</span>
    listen(el, <span class="hljs-string">&apos;change&apos;</span>, () =&gt; {
      assign(getValue(el))
    })

    <span class="hljs-comment">// &#x76D1;&#x542C;&#x72B6;&#x6001;&#x503C;&#x53D8;&#x5316;&#xFF0C;&#x66F4;&#x65B0;&#x63A7;&#x4EF6;&#x503C;</span>
    <span class="hljs-keyword">let</span> oldValue: <span class="hljs-built_in">any</span>
    effect(() =&gt; {
      <span class="hljs-keyword">const</span> value = <span class="hljs-keyword">get</span>()
      <span class="hljs-keyword">if</span> (value !== oldValue) {
        ;(el as HTMLInputElement).checked = looseEqual(value, getValue(el))
      }
    })
  }
  <span class="hljs-keyword">else</span> {
    <span class="hljs-comment">// input[type=text], textarea, div[contenteditable=true]</span>
    <span class="hljs-keyword">const</span> resolveValue = (value: <span class="hljs-built_in">string</span>) =&gt; {
      <span class="hljs-keyword">if</span> (trim) <span class="hljs-keyword">return</span> val.trim()
      <span class="hljs-keyword">if</span> (<span class="hljs-built_in">number</span>) <span class="hljs-keyword">return</span> toNumber(val)
      <span class="hljs-keyword">return</span> val
    }

    <span class="hljs-comment">// &#x76D1;&#x542C;&#x662F;&#x5426;&#x5728;&#x8F93;&#x5165;&#x6CD5;&#x7F16;&#x8F91;&#x5668;(input method editor)&#x8F93;&#x5165;&#x5185;&#x5BB9;</span>
    listen(el, <span class="hljs-string">&apos;compositionstart&apos;</span>, onCompositionStart)
    listen(el, <span class="hljs-string">&apos;compositionend&apos;</span>, onCompositionEnd)
    <span class="hljs-comment">// change&#x4E8B;&#x4EF6;&#x662F;&#x5143;&#x7D20;&#x5931;&#x7126;&#x540E;&#x524D;&#x540E;&#x503C;&#x4E0D;&#x540C;&#x65F6;&#x89E6;&#x53D1;&#xFF0C;&#x800C;input&#x4E8B;&#x4EF6;&#x662F;&#x8F93;&#x5165;&#x8FC7;&#x7A0B;&#x4E2D;&#x6BCF;&#x6B21;&#x4FEE;&#x6539;&#x503C;&#x90FD;&#x4F1A;&#x89E6;&#x53D1;</span>
    listen(el, modifiers?.lazy ? <span class="hljs-string">&apos;change&apos;</span> : <span class="hljs-string">&apos;input&apos;</span>, () =&gt; {
      <span class="hljs-comment">// &#x5143;&#x7D20;&#x7684;composing&#x5C5E;&#x6027;&#x7528;&#x4E8E;&#x6807;&#x8BB0;&#x662F;&#x5426;&#x5904;&#x4E8E;&#x8F93;&#x5165;&#x6CD5;&#x7F16;&#x8F91;&#x5668;&#x8F93;&#x5165;&#x5185;&#x5BB9;&#x7684;&#x72B6;&#x6001;&#xFF0C;&#x5982;&#x679C;&#x662F;&#x5219;&#x4E0D;&#x6267;&#x884C;change&#x6216;input&#x4E8B;&#x4EF6;&#x7684;&#x903B;&#x8F91;</span>
      <span class="hljs-keyword">if</span> ((el as <span class="hljs-built_in">any</span>).composing) <span class="hljs-keyword">return</span>
      assign(resolveValue(el.value))
    })
    <span class="hljs-keyword">if</span> (trim) {
      <span class="hljs-comment">// &#x82E5;modifiers.trim&#xFF0C;&#x90A3;&#x4E48;&#x5F53;&#x5143;&#x7D20;&#x5931;&#x7126;&#x65F6;&#x9A6C;&#x4E0A;&#x79FB;&#x9664;&#x503C;&#x524D;&#x540E;&#x7684;&#x7A7A;&#x683C;&#x5B57;&#x7B26;</span>
      listen(el, <span class="hljs-string">&apos;change&apos;</span>, () =&gt; {
        el.value = el.value.trim()
      })
    }

    effect(() =&gt; {
      <span class="hljs-keyword">if</span> ((el as <span class="hljs-built_in">any</span>).composing) {
        <span class="hljs-keyword">return</span>
      }
      <span class="hljs-keyword">const</span> curVal = el.value
      <span class="hljs-keyword">const</span> newVal = <span class="hljs-keyword">get</span>()
      <span class="hljs-comment">// &#x82E5;&#x5F53;&#x524D;&#x5143;&#x7D20;&#x5904;&#x4E8E;&#x6D3B;&#x52A8;&#x72B6;&#x6001;&#xFF08;&#x5373;&#x5F97;&#x5230;&#x7126;&#x70B9;&#xFF09;&#xFF0C;&#x5E76;&#x4E14;&#x5143;&#x7D20;&#x5F53;&#x524D;&#x503C;&#x8FDB;&#x884C;&#x7C7B;&#x578B;&#x8F6C;&#x6362;&#x540E;&#x503C;&#x4E0E;&#x65B0;&#x503C;&#x76F8;&#x540C;&#xFF0C;&#x5219;&#x4E0D;&#x7528;&#x8D4B;&#x503C;&#xFF1B;</span>
      <span class="hljs-comment">// &#x5426;&#x5219;&#x53EA;&#x8981;&#x5143;&#x7D20;&#x5F53;&#x524D;&#x503C;&#x548C;&#x65B0;&#x503C;&#x7C7B;&#x578B;&#x6216;&#x503C;&#x4E0D;&#x76F8;&#x540C;&#xFF0C;&#x90FD;&#x4F1A;&#x91CD;&#x65B0;&#x8D4B;&#x503C;&#x3002;&#x90A3;&#x4E48;&#x82E5;&#x65B0;&#x503C;&#x4E3A;&#x6570;&#x7EC4;[1,2,3]&#xFF0C;&#x8D4B;&#x503C;&#x540E;&#x5143;&#x7D20;&#x7684;&#x503C;&#x5C06;&#x53D8;&#x6210;[object Array]</span>
      <span class="hljs-keyword">if</span> (<span class="hljs-built_in">document</span>.activeElement === el &amp;&amp; resolveValue(curVal) === newVal) {
        <span class="hljs-keyword">return</span>
      }
      <span class="hljs-keyword">if</span> (curVal !== newVal) {
        el.value = newVal
      }
    })
  }
}

<span class="hljs-comment">// v-bind&#x4E2D;&#x4F7F;&#x7528;_value&#x5C5E;&#x6027;&#x4FDD;&#x5B58;&#x4EFB;&#x610F;&#x7C7B;&#x578B;&#x7684;&#x503C;&#xFF0C;&#x5728;v-modal&#x4E2D;&#x8BFB;&#x53D6;</span>
<span class="hljs-keyword">const</span> getValue = (el: <span class="hljs-built_in">any</span>) =&gt; (<span class="hljs-string">&apos;_value&apos;</span> <span class="hljs-keyword">in</span> el ? el._value : el.value)

<span class="hljs-keyword">const</span> getCheckboxValue = (
  el: HTMLInputElement &amp; {_trueValue?: <span class="hljs-built_in">any</span>, _falseValue?: <span class="hljs-built_in">any</span>}, <span class="hljs-comment">// &#x901A;&#x8FC7;v-bind&#x5B9A;&#x4E49;&#x7684;&#x4EFB;&#x610F;&#x7C7B;&#x578B;&#x503C;</span>
  checked: <span class="hljs-built_in">boolean</span> <span class="hljs-comment">// checkbox&#x7684;&#x9ED8;&#x8BA4;&#x503C;&#x662F;true&#x548C;false</span>
) =&gt; {
  <span class="hljs-keyword">const</span> key = checked ? <span class="hljs-string">&apos;_trueValue&apos;</span> : <span class="hljs-string">&apos;_falseValue&apos;</span>
  <span class="hljs-keyword">return</span> key <span class="hljs-keyword">in</span> el ? el[key] : checked
}

<span class="hljs-keyword">const</span> onCompositionStart = (e: Event) =&gt; {
  <span class="hljs-comment">// &#x901A;&#x8FC7;&#x81EA;&#x5B9A;&#x4E49;&#x5143;&#x7D20;&#x7684;composing&#x5143;&#x7D20;&#xFF0C;&#x7528;&#x4E8E;&#x6807;&#x8BB0;&#x662F;&#x5426;&#x5728;&#x8F93;&#x5165;&#x6CD5;&#x7F16;&#x8F91;&#x5668;&#x4E2D;&#x8F93;&#x5165;&#x5185;&#x5BB9;</span>
  ;(e.target as <span class="hljs-built_in">any</span>).composing = <span class="hljs-literal">true</span>
}  

<span class="hljs-keyword">const</span> onCompositionEnd = (e: Event) =&gt; {
  <span class="hljs-keyword">const</span> target = e.target as <span class="hljs-built_in">any</span>
  <span class="hljs-keyword">if</span> (target.composing) {
    <span class="hljs-comment">// &#x624B;&#x52A8;&#x89E6;&#x53D1;input&#x4E8B;&#x4EF6;</span>
    target.composing = <span class="hljs-literal">false</span>
    trigger(target, <span class="hljs-string">&apos;input&apos;</span>)
  }
}

<span class="hljs-keyword">const</span> trigger = (el: HTMLElement, <span class="hljs-keyword">type</span>: <span class="hljs-built_in">string</span>) =&gt; {
  <span class="hljs-keyword">const</span> e = <span class="hljs-built_in">document</span>.createEvent(<span class="hljs-string">&apos;HTMLEvents&apos;</span>)
  e.initEvent(<span class="hljs-keyword">type</span>, <span class="hljs-literal">true</span>, <span class="hljs-literal">true</span>)
  el.dispatchEvent(e)
}
</code></pre>
<h2 id="compositionstart&#x548C;compositionend&#x662F;&#x4EC0;&#x4E48;&#xFF1F;"><a name="compositionstart&#x548C;compositionend&#x662F;&#x4EC0;&#x4E48;&#xFF1F;" class="anchor-navigation-ex-anchor" href="#compositionstart&#x548C;compositionend&#x662F;&#x4EC0;&#x4E48;&#xFF1F;"><i class="fa fa-link" aria-hidden="true"></i></a>1.3. compositionstart&#x548C;compositionend&#x662F;&#x4EC0;&#x4E48;&#xFF1F;</h2>
<p><code>compositionstart</code>&#x662F;&#x5F00;&#x59CB;&#x5728;&#x8F93;&#x5165;&#x6CD5;&#x7F16;&#x8F91;&#x5668;&#x4E0A;&#x8F93;&#x5165;&#x5B57;&#x7B26;&#x89E6;&#x53D1;&#xFF0C;&#x800C;<code>compositionend</code>&#x5219;&#x662F;&#x5728;&#x8F93;&#x5165;&#x6CD5;&#x7F16;&#x8F91;&#x5668;&#x4E0A;&#x8F93;&#x5165;&#x5B57;&#x7B26;&#x7ED3;&#x675F;&#x65F6;&#x89E6;&#x53D1;&#xFF0C;&#x53E6;&#x5916;&#x8FD8;&#x6709;&#x4E00;&#x4E2A;<code>compositionupdate</code>&#x662F;&#x5728;&#x8F93;&#x5165;&#x6CD5;&#x7F16;&#x8F91;&#x5668;&#x4E0A;&#x8F93;&#x5165;&#x5B57;&#x7B26;&#x8FC7;&#x7A0B;&#x4E2D;&#x89E6;&#x53D1;&#x3002;</p>
<p>&#x5F53;&#x6211;&#x4EEC;&#x5728;&#x8F93;&#x5165;&#x6CD5;&#x7F16;&#x8F91;&#x5668;&#x6572;&#x51FB;&#x952E;&#x76D8;&#x65F6;&#x4F1A;&#x6309;&#x987A;&#x5E8F;&#x6267;&#x884C;&#x5982;&#x4E0B;&#x4E8B;&#x4EF6;&#xFF1A;
<code>compositionstart</code> -&gt; (<code>compositionupdate</code> -&gt; <code>input</code>)+ -&gt; <code>compositionend</code> -&gt; &#x5F53;&#x5931;&#x7126;&#x65F6;&#x89E6;&#x53D1;<code>change</code>
&#x5F53;&#x5728;&#x8F93;&#x5165;&#x6CD5;&#x7F16;&#x8F91;&#x5668;&#x4E0A;&#x8F93;&#x5165;<code>ri</code>&#x540E;&#x6309;&#x7A7A;&#x683C;&#x786E;&#x8BA4;<code>&#x65E5;</code>&#x5B57;&#x7B26;&#xFF0C;&#x5219;&#x89E6;&#x53D1;&#x5982;&#x4E0B;&#x4E8B;&#x4EF6;
<code>compositionstart(data=&quot;&quot;)</code> -&gt; <code>compositionupdate(data=&quot;r&quot;)</code> -&gt; <code>input</code> -&gt; <code>compositionupdate(data=&quot;ri&quot;)</code> -&gt; <code>input</code> -&gt; <code>compositionupdate(data=&quot;&#x65E5;&quot;)</code> -&gt; <code>input</code> -&gt; <code>compositionend(data=&quot;&#x65E5;&quot;)</code></p>
<p>&#x7531;&#x4E8E;&#x5728;&#x8F93;&#x5165;&#x6CD5;&#x7F16;&#x8F91;&#x5668;&#x4E0A;&#x8F93;&#x5165;&#x5B57;&#x7B26;&#x65F6;&#x4F1A;&#x89E6;&#x53D1;<code>input</code>&#x4E8B;&#x4EF6;&#xFF0C;&#x6240;&#x4EE5;petite-vue&#x4E2D;&#x901A;&#x8FC7;&#x5728;&#x5BF9;&#x8C61;&#x4E0A;&#x8BBE;&#x7F6E;<code>composing</code>&#x6807;&#x8BC6;&#x662F;&#x5426;&#x6267;&#x884C;<code>input</code>&#x903B;&#x8F91;&#x3002;</p>
<p>&#x4E8B;&#x4EF6;&#x5BF9;&#x8C61;&#x5C5E;&#x6027;&#x5982;&#x4E0B;&#xFF1A;</p>
<pre><code class="lang-ts">readonly target: EventTarget <span class="hljs-comment">// &#x6307;&#x5411;&#x89E6;&#x53D1;&#x4E8B;&#x4EF6;&#x7684;HTML&#x5143;&#x7D20;</span>
readolny <span class="hljs-keyword">type</span>: DOMString <span class="hljs-comment">// &#x4E8B;&#x4EF6;&#x540D;&#x79F0;&#xFF0C;&#x5373;compositionstart&#x6216;compositionend</span>
readonly bubbles: <span class="hljs-built_in">boolean</span> <span class="hljs-comment">// &#x4E8B;&#x4EF6;&#x662F;&#x5426;&#x5192;&#x6CE1;</span>
readonly cancelable: <span class="hljs-built_in">boolean</span> <span class="hljs-comment">// &#x4E8B;&#x4EF6;&#x662F;&#x5426;&#x53EF;&#x53D6;&#x6D88;</span>
readonly view: WindowProxy <span class="hljs-comment">// &#x5F53;&#x524D;&#x6587;&#x6863;&#x5BF9;&#x8C61;&#x6240;&#x5C5E;&#x7684;window&#x5BF9;&#x8C61;(`document.defaultView`)</span>
readonly detail: long
readonly data: DOMString <span class="hljs-comment">// &#x6700;&#x7EC8;&#x586B;&#x5199;&#x5230;&#x5143;&#x7D20;&#x7684;&#x5185;&#x5BB9;&#xFF0C;compositionstart&#x4E3A;&#x7A7A;&#xFF0C;compositionend&#x4E8B;&#x4EF6;&#x4E2D;&#x80FD;&#x83B7;&#x53D6;&#x5982;&quot;&#x4F60;&#x597D;&quot;&#x7684;&#x5185;&#x5BB9;</span>
readonly locale: DOMString
</code></pre>
<h2 id="&#x7F16;&#x7801;&#x65B9;&#x5F0F;&#x89E6;&#x53D1;&#x4E8B;&#x4EF6;"><a name="&#x7F16;&#x7801;&#x65B9;&#x5F0F;&#x89E6;&#x53D1;&#x4E8B;&#x4EF6;" class="anchor-navigation-ex-anchor" href="#&#x7F16;&#x7801;&#x65B9;&#x5F0F;&#x89E6;&#x53D1;&#x4E8B;&#x4EF6;"><i class="fa fa-link" aria-hidden="true"></i></a>1.4. &#x7F16;&#x7801;&#x65B9;&#x5F0F;&#x89E6;&#x53D1;&#x4E8B;&#x4EF6;</h2>
<p>DOM Level2&#x7684;&#x4E8B;&#x4EF6;&#x4E2D;&#x5305;&#x542B;HTMLEvents, MouseEvents&#x3001;MutationEvents&#x548C;UIEvents&#xFF0C;&#x800C;DOM Level3&#x5219;&#x589E;&#x52A0;&#x5982;CustomEvent&#x7B49;&#x4E8B;&#x4EF6;&#x7C7B;&#x578B;&#x3002;</p>
<pre><code class="lang-ts"><span class="hljs-keyword">enum</span> EventType {
  <span class="hljs-comment">// DOM Level 2 Events</span>
  UIEvents,
  MouseEvents, <span class="hljs-comment">// event.initMouseEvent</span>
  MutationEvents, <span class="hljs-comment">// event.initMutationEvent</span>
  HTMLEvents, <span class="hljs-comment">// event.initEvent</span>
  <span class="hljs-comment">// DOM Level 3 Events</span>
  UIEvent,
  MouseEvent, <span class="hljs-comment">// event.initMouseEvent</span>
  MutationEvent, <span class="hljs-comment">// event.initMutationEvent</span>
  TextEvent, <span class="hljs-comment">// TextEvents is also supported, event.initTextEvent</span>
  KeyboardEvent, <span class="hljs-comment">// KeyEvents is also supported, use `new KeyboardEvent()` to create keyboard event</span>
  CustomEvent, <span class="hljs-comment">// event.initCustomEvent</span>
  Event, <span class="hljs-comment">// Basic events module, event.initEvent</span>
}
</code></pre>
<ul>
<li>HTMLEvents&#x5305;&#x542B;<code>abort</code>, <code>blur</code>, <code>change</code>, <code>error</code>, <code>focus</code>, <code>load</code>, <code>reset</code>, <code>resize</code>, <code>scroll</code>, <code>select</code>, <code>submit</code>, <code>unload</code>, <code>input</code></li>
<li>UIEvents&#x5305;&#x542B;<code>DOMActive</code>, <code>DOMFocusIn</code>, <code>DOMFocusOut</code>, <code>keydown</code>, <code>keypress</code>, <code>keyup</code></li>
<li>MouseEvents&#x5305;&#x542B;<code>click</code>, <code>mousedown</code>, <code>mousemove</code>, <code>mouseout</code>, <code>mouseover</code>, <code>mouseup</code></li>
<li>MutationEvents&#x5305;&#x542B;<code>DOMAttrModified</code>,<code>DOMNodeInserted</code>,<code>DOMNodeRemoved</code>,<code>DOMCharacterDataModified</code>,<code>DOMNodeInsertedIntoDocument</code>,<code>DOMNodeRemovedFromDocument</code>,<code>DOMSubtreeModified</code></li>
</ul>
<h3 id="&#x521B;&#x5EFA;&#x548C;&#x521D;&#x59CB;&#x5316;&#x4E8B;&#x4EF6;&#x5BF9;&#x8C61;"><a name="&#x521B;&#x5EFA;&#x548C;&#x521D;&#x59CB;&#x5316;&#x4E8B;&#x4EF6;&#x5BF9;&#x8C61;" class="anchor-navigation-ex-anchor" href="#&#x521B;&#x5EFA;&#x548C;&#x521D;&#x59CB;&#x5316;&#x4E8B;&#x4EF6;&#x5BF9;&#x8C61;"><i class="fa fa-link" aria-hidden="true"></i></a>1.4.1. &#x521B;&#x5EFA;&#x548C;&#x521D;&#x59CB;&#x5316;&#x4E8B;&#x4EF6;&#x5BF9;&#x8C61;</h3>
<h4 id="mouseevent"><a name="mouseevent" class="anchor-navigation-ex-anchor" href="#mouseevent"><i class="fa fa-link" aria-hidden="true"></i></a>MouseEvent</h4>
<p>&#x65B9;&#x6CD5;1</p>
<pre><code class="lang-ts"><span class="hljs-keyword">const</span> e: Event = <span class="hljs-built_in">document</span>.createEvent(<span class="hljs-string">&apos;MouseEvent&apos;</span>)
e.initMouseEvent(
  <span class="hljs-keyword">type</span>: <span class="hljs-built_in">string</span>,
  bubbles: <span class="hljs-built_in">boolean</span>,
  cancelable: <span class="hljs-built_in">boolean</span>,
  view: AbstractView, <span class="hljs-comment">// &#x6307;&#x5411;&#x4E0E;&#x4E8B;&#x4EF6;&#x76F8;&#x5173;&#x7684;&#x89C6;&#x56FE;&#xFF0C;&#x4E00;&#x822C;&#x4E3A;document.defaultView</span>
  detail: <span class="hljs-built_in">number</span>, <span class="hljs-comment">// &#x4F9B;&#x4E8B;&#x4EF6;&#x56DE;&#x8C03;&#x51FD;&#x6570;&#x4F7F;&#x7528;&#xFF0C;&#x4E00;&#x822C;&#x4E3A;0</span>
  screenX: <span class="hljs-built_in">number</span>, <span class="hljs-comment">// &#x76F8;&#x5BF9;&#x4E8E;&#x5C4F;&#x5E55;&#x7684;x&#x5750;&#x6807;</span>
  screenY: <span class="hljs-built_in">number</span>, <span class="hljs-comment">// &#x76F8;&#x5BF9;&#x4E8E;&#x5C4F;&#x5E55;&#x7684;Y&#x5750;&#x6807;</span>
  clientX: <span class="hljs-built_in">number</span>, <span class="hljs-comment">// &#x76F8;&#x5BF9;&#x4E8E;&#x89C6;&#x53E3;&#x7684;x&#x5750;&#x6807;</span>
  clientY: <span class="hljs-built_in">number</span>, <span class="hljs-comment">// &#x76F8;&#x5BF9;&#x4E8E;&#x89C6;&#x53E3;&#x7684;Y&#x5750;&#x6807;</span>
  ctrlKey: <span class="hljs-built_in">boolean</span>, <span class="hljs-comment">// &#x662F;&#x5426;&#x6309;&#x4E0B;Ctrl&#x952E;</span>
  altKey: <span class="hljs-built_in">boolean</span>, <span class="hljs-comment">// &#x662F;&#x5426;&#x6309;&#x4E0B;Ctrl&#x952E;</span>
  shiftKey: <span class="hljs-built_in">boolean</span>, <span class="hljs-comment">// &#x662F;&#x5426;&#x6309;&#x4E0B;Ctrl&#x952E;</span>
  metaKey: <span class="hljs-built_in">boolean</span>, <span class="hljs-comment">// &#x662F;&#x5426;&#x6309;&#x4E0B;Ctrl&#x952E;</span>
  button: <span class="hljs-built_in">number</span>, <span class="hljs-comment">// &#x6309;&#x4E0B;&#x6309;&#x4E2A;&#x9F20;&#x6807;&#x952E;&#xFF0C;&#x9ED8;&#x8BA4;&#x4E3A;0.0&#x5DE6;&#xFF0C;1&#x4E2D;&#xFF0C;2&#x53F3;</span>
  relatedTarget: HTMLElement <span class="hljs-comment">// &#x6307;&#x5411;&#x4E8E;&#x4E8B;&#x4EF6;&#x76F8;&#x5173;&#x7684;&#x5143;&#x7D20;&#xFF0C;&#x4E00;&#x822C;&#x53EA;&#x6709;&#x5728;&#x6A21;&#x62DF;mouseover&#x548C;mouseout&#x65F6;&#x4F7F;&#x7528;</span>
)
</code></pre>
<p>&#x65B9;&#x6CD5;2</p>
<pre><code class="lang-ts"><span class="hljs-keyword">const</span> e: Event = <span class="hljs-keyword">new</span> MouseEvent(<span class="hljs-string">&apos;click&apos;</span>, {
  bubbles: <span class="hljs-literal">false</span>,
  <span class="hljs-comment">// ......</span>
})
</code></pre>
<h4 id="keyboardevent"><a name="keyboardevent" class="anchor-navigation-ex-anchor" href="#keyboardevent"><i class="fa fa-link" aria-hidden="true"></i></a>KeyboardEvent</h4>
<pre><code class="lang-ts"><span class="hljs-keyword">const</span> e = <span class="hljs-keyword">new</span> KeyboardEvent(
  typeArg: <span class="hljs-built_in">string</span>, <span class="hljs-comment">// &#x5982;keypress</span>
  {
    ctrlKey: <span class="hljs-literal">true</span>,
    <span class="hljs-comment">// ......</span>
  }
)
</code></pre>
<p><a href="https://developer.mozilla.org/en-US/docs/Web/API/KeyboardEvent/KeyboardEvent" target="_blank">https://developer.mozilla.org/en-US/docs/Web/API/KeyboardEvent/KeyboardEvent</a></p>
<h4 id="event&#x7684;&#x521D;&#x59CB;&#x65B9;&#x6CD5;"><a name="event&#x7684;&#x521D;&#x59CB;&#x65B9;&#x6CD5;" class="anchor-navigation-ex-anchor" href="#event&#x7684;&#x521D;&#x59CB;&#x65B9;&#x6CD5;"><i class="fa fa-link" aria-hidden="true"></i></a>Event&#x7684;&#x521D;&#x59CB;&#x65B9;&#x6CD5;</h4>
<pre><code class="lang-ts"><span class="hljs-comment">/**
 * &#x9009;&#x9879;&#x7684;&#x5C5E;&#x6027;
 * @param {string} name - &#x4E8B;&#x4EF6;&#x540D;&#x79F0;, &#x5982;click,input&#x7B49;
 * @param {boolean} [cancelable=false] - &#x6307;&#x5B9A;&#x4E8B;&#x4EF6;&#x662F;&#x5426;&#x53EF;&#x5192;&#x6CE1;
 * @param {boolean} [cancelable=false] - &#x6307;&#x5B9A;&#x4E8B;&#x4EF6;&#x662F;&#x5426;&#x53EF;&#x88AB;&#x53D6;&#x6D88;
 * @param {boolean} [composed=false] - &#x6307;&#x5B9A;&#x4E8B;&#x4EF6;&#x662F;&#x5426;&#x4F1A;&#x5728;Shadow DOM&#x6839;&#x8282;&#x70B9;&#x5916;&#x89E6;&#x53D1;&#x4E8B;&#x4EF6;&#x56DE;&#x8C03;&#x51FD;&#x6570;
 */</span>
<span class="hljs-keyword">const</span> e = <span class="hljs-keyword">new</span> Event(<span class="hljs-string">&apos;input&apos;</span>, {
  name: <span class="hljs-built_in">string</span>, 
  bubbles: <span class="hljs-built_in">boolean</span> = <span class="hljs-literal">false</span>, 
  cancelable: <span class="hljs-built_in">boolean</span> = <span class="hljs-literal">false</span>, 
  composed: <span class="hljs-built_in">boolean</span> = <span class="hljs-literal">false</span>
})
</code></pre>
<h4 id="customevent"><a name="customevent" class="anchor-navigation-ex-anchor" href="#customevent"><i class="fa fa-link" aria-hidden="true"></i></a>CustomEvent</h4>
<p>&#x65B9;&#x6CD5;1</p>
<pre><code class="lang-ts"><span class="hljs-keyword">const</span> e: Event = <span class="hljs-built_in">document</span>.createEvent(<span class="hljs-string">&apos;CustomEvent&apos;</span>)
e.initMouseEvent(
  <span class="hljs-keyword">type</span>: <span class="hljs-built_in">string</span>,
  bubbles: <span class="hljs-built_in">boolean</span>,
  cancelable: <span class="hljs-built_in">boolean</span>,
  detail: <span class="hljs-built_in">any</span>
)
</code></pre>
<p>&#x65B9;&#x6CD5;2</p>
<pre><code class="lang-ts"><span class="hljs-comment">/**
 * &#x9009;&#x9879;&#x7684;&#x5C5E;&#x6027;
 * @param {string} name - &#x4E8B;&#x4EF6;&#x540D;&#x79F0;, &#x5982;click,input&#x7B49;&#xFF0C;&#x53EF;&#x968F;&#x610F;&#x5B9A;&#x4E49;
 * @param {boolean} [cancelable=false] - &#x6307;&#x5B9A;&#x4E8B;&#x4EF6;&#x662F;&#x5426;&#x53EF;&#x5192;&#x6CE1;
 * @param {boolean} [cancelable=false] - &#x6307;&#x5B9A;&#x4E8B;&#x4EF6;&#x662F;&#x5426;&#x53EF;&#x88AB;&#x53D6;&#x6D88;
 * @param {any} [detail=null] - &#x4E8B;&#x4EF6;&#x521D;&#x59CB;&#x5316;&#x65F6;&#x4F20;&#x9012;&#x7684;&#x6570;&#x636E;
 */</span>
<span class="hljs-keyword">const</span> e = <span class="hljs-keyword">new</span> CustomEvent(<span class="hljs-string">&apos;hi&apos;</span>, {
  name: <span class="hljs-built_in">string</span>, 
  bubbles: <span class="hljs-built_in">boolean</span> = <span class="hljs-literal">false</span>, 
  cancelable: <span class="hljs-built_in">boolean</span> = <span class="hljs-literal">false</span>, 
  detail: <span class="hljs-built_in">any</span> = <span class="hljs-literal">null</span>
})
</code></pre>
<h4 id="htmlevents"><a name="htmlevents" class="anchor-navigation-ex-anchor" href="#htmlevents"><i class="fa fa-link" aria-hidden="true"></i></a>HTMLEvents</h4>
<pre><code class="lang-ts"><span class="hljs-keyword">const</span> e: Event = <span class="hljs-built_in">document</span>.createEvent(<span class="hljs-string">&apos;HTMLEvents&apos;</span>)
e.initMouseEvent(
  <span class="hljs-keyword">type</span>: <span class="hljs-built_in">string</span>,
  bubbles: <span class="hljs-built_in">boolean</span>,
  cancelable: <span class="hljs-built_in">boolean</span>
)
</code></pre>
<h3 id="&#x6DFB;&#x52A0;&#x76D1;&#x542C;&#x548C;&#x53D1;&#x5E03;&#x4E8B;&#x4EF6;"><a name="&#x6DFB;&#x52A0;&#x76D1;&#x542C;&#x548C;&#x53D1;&#x5E03;&#x4E8B;&#x4EF6;" class="anchor-navigation-ex-anchor" href="#&#x6DFB;&#x52A0;&#x76D1;&#x542C;&#x548C;&#x53D1;&#x5E03;&#x4E8B;&#x4EF6;"><i class="fa fa-link" aria-hidden="true"></i></a>1.4.2. &#x6DFB;&#x52A0;&#x76D1;&#x542C;&#x548C;&#x53D1;&#x5E03;&#x4E8B;&#x4EF6;</h3>
<pre><code class="lang-ts">element.addEventListener(<span class="hljs-keyword">type</span>: <span class="hljs-built_in">string</span>)
element.dispatchEvent(e: Event)
</code></pre>
<h3 id="&#x9488;&#x5BF9;petite-vue&#x8FDB;&#x884C;&#x5206;&#x6790;"><a name="&#x9488;&#x5BF9;petite-vue&#x8FDB;&#x884C;&#x5206;&#x6790;" class="anchor-navigation-ex-anchor" href="#&#x9488;&#x5BF9;petite-vue&#x8FDB;&#x884C;&#x5206;&#x6790;"><i class="fa fa-link" aria-hidden="true"></i></a>1.4.3. &#x9488;&#x5BF9;petite-vue&#x8FDB;&#x884C;&#x5206;&#x6790;</h3>
<pre><code class="lang-ts"><span class="hljs-keyword">const</span> onCompositionEnd = (e: Event) =&gt; {
  <span class="hljs-keyword">const</span> target = e.target as <span class="hljs-built_in">any</span>
  <span class="hljs-keyword">if</span> (target.composing) {
    <span class="hljs-comment">// &#x624B;&#x52A8;&#x89E6;&#x53D1;input&#x4E8B;&#x4EF6;</span>
    target.composing = <span class="hljs-literal">false</span>
    trigger(target, <span class="hljs-string">&apos;input&apos;</span>)
  }
}
<span class="hljs-keyword">const</span> trigger = (el: HTMLElement, <span class="hljs-keyword">type</span>: <span class="hljs-built_in">string</span>) =&gt; {
  <span class="hljs-keyword">const</span> e = <span class="hljs-built_in">document</span>.createEvent(<span class="hljs-string">&apos;HTMLEvents&apos;</span>)
  e.initEvent(<span class="hljs-keyword">type</span>, <span class="hljs-literal">true</span>, <span class="hljs-literal">true</span>)
  el.dispatchEvent(e)
}
</code></pre>
<p>&#x5F53;&#x5728;&#x8F93;&#x5165;&#x6CD5;&#x7F16;&#x8F91;&#x5668;&#x64CD;&#x4F5C;&#x5B8C;&#x6BD5;&#x540E;&#x4F1A;&#x624B;&#x52A8;&#x89E6;&#x53D1;input&#x4E8B;&#x4EF6;&#xFF0C;&#x4F46;&#x5F53;&#x4E8B;&#x4EF6;&#x7ED1;&#x5B9A;&#x4FEE;&#x9970;&#x7B26;&#x8BBE;&#x7F6E;&#x4E3A;<code>lazy</code>&#x540E;&#x5E76;&#x6CA1;&#x6709;&#x7ED1;&#x5B9A;<code>input</code>&#x4E8B;&#x4EF6;&#x56DE;&#x8C03;&#x51FD;&#x6570;&#xFF0C;&#x6B64;&#x65F6;&#x5728;&#x8F93;&#x5165;&#x6CD5;&#x7F16;&#x8F91;&#x5668;&#x64CD;&#x4F5C;&#x5B8C;&#x6BD5;&#x540E;&#x5E76;&#x4E0D;&#x4F1A;&#x81EA;&#x52A8;&#x66F4;&#x65B0;&#x72B6;&#x6001;&#xFF0C;&#x56E0;&#x4E3A;&#x90FD;&#x8981;&#x7B49;&#x5F85;&#x8F93;&#x5165;&#x5143;&#x7D20;&#x5931;&#x53BB;&#x7126;&#x70B9;&#x65F6;&#x624D;&#x4F1A;&#x89E6;&#x53D1;:)</p>
<pre><code class="lang-ts"><span class="hljs-comment">// change&#x4E8B;&#x4EF6;&#x662F;&#x5143;&#x7D20;&#x5931;&#x7126;&#x540E;&#x524D;&#x540E;&#x503C;&#x4E0D;&#x540C;&#x65F6;&#x89E6;&#x53D1;&#xFF0C;&#x800C;input&#x4E8B;&#x4EF6;&#x662F;&#x8F93;&#x5165;&#x8FC7;&#x7A0B;&#x4E2D;&#x6BCF;&#x6B21;&#x4FEE;&#x6539;&#x503C;&#x90FD;&#x4F1A;&#x89E6;&#x53D1;</span>
    listen(el, modifiers?.lazy ? <span class="hljs-string">&apos;change&apos;</span> : <span class="hljs-string">&apos;input&apos;</span>, () =&gt; {
      <span class="hljs-comment">// &#x5143;&#x7D20;&#x7684;composing&#x5C5E;&#x6027;&#x7528;&#x4E8E;&#x6807;&#x8BB0;&#x662F;&#x5426;&#x5904;&#x4E8E;&#x8F93;&#x5165;&#x6CD5;&#x7F16;&#x8F91;&#x5668;&#x8F93;&#x5165;&#x5185;&#x5BB9;&#x7684;&#x72B6;&#x6001;&#xFF0C;&#x5982;&#x679C;&#x662F;&#x5219;&#x4E0D;&#x6267;&#x884C;change&#x6216;input&#x4E8B;&#x4EF6;&#x7684;&#x903B;&#x8F91;</span>
      <span class="hljs-keyword">if</span> ((el as <span class="hljs-built_in">any</span>).composing) <span class="hljs-keyword">return</span>
      assign(resolveValue(el.value))
    })
</code></pre>
<h3 id="&#x5916;&#x756A;&#xFF1A;ie&#x7684;&#x4E8B;&#x4EF6;&#x6A21;&#x62DF;"><a name="&#x5916;&#x756A;&#xFF1A;ie&#x7684;&#x4E8B;&#x4EF6;&#x6A21;&#x62DF;" class="anchor-navigation-ex-anchor" href="#&#x5916;&#x756A;&#xFF1A;ie&#x7684;&#x4E8B;&#x4EF6;&#x6A21;&#x62DF;"><i class="fa fa-link" aria-hidden="true"></i></a>1.4.4. &#x5916;&#x756A;&#xFF1A;IE&#x7684;&#x4E8B;&#x4EF6;&#x6A21;&#x62DF;</h3>
<pre><code class="lang-js"><span class="hljs-keyword">var</span> e = <span class="hljs-built_in">document</span>.createEventObject()
e.shiftKey = <span class="hljs-literal">false</span>
e.button = <span class="hljs-number">0</span>
<span class="hljs-built_in">document</span>.getElementById(<span class="hljs-string">&apos;click&apos;</span>).fireEvent(<span class="hljs-string">&apos;onclick&apos;</span>, e)
</code></pre>
<script type="text/javascript">
  /**
   * Donation
   */
  function showQR(){
    var rewardButton = document.getElementById('rewardButton')
    var qr = document.getElementById('QR')
    if (rewardButton) {
      rewardButton.style.display = 'none'
    }
    if (qr) {
      qr.style.display = 'block'
    }
  }
  setInterval(showQR, 1000)

  /**
   * Baidu analysis
   */
  var _hmt = _hmt || [];
  (function() {
    var hm = document.createElement("script");
    hm.src = "https://hm.baidu.com/hm.js?8bca1e3aa362a7f85dbe8304cff65225";
    var s = document.getElementsByTagName("script")[0]; 
    s.parentNode.insertBefore(hm, s);
  })();
</script>


<footer class="page-footer"><span class="copyright">Copyright &#xA9; fsjohnhuang 2022 all right reserved&#xFF0C;powered by Gitbook</span><span class="footer-modification">File Modify: 
2022-04-26 09:46:57
</span></footer>
                                
                                </section>
                            
    </div>
    <div class="search-results">
        <div class="has-results">
            
            <h1 class="search-results-title"><span class='search-results-count'></span> results matching "<span class='search-query'></span>"</h1>
            <ul class="search-results-list"></ul>
            
        </div>
        <div class="no-results">
            
            <h1 class="search-results-title">No results matching "<span class='search-query'></span>"</h1>
            
        </div>
    </div>
</div>

                        </div>
                    </div>
                
            </div>

            
                
                <a href="2_event_binding.html" class="navigation navigation-prev " aria-label="Previous page: 事件绑定v-on的工作原理">
                    <i class="fa fa-angle-left"></i>
                </a>
                
                
                <a href="4_ref.html" class="navigation navigation-next " aria-label="Next page: 特殊指令ref的工作原理">
                    <i class="fa fa-angle-right"></i>
                </a>
                
            
        
    </div>

    <script>
        var gitbook = gitbook || [];
        gitbook.push(function() {
            gitbook.page.hasChanged({"page":{"title":"双向绑定v-model的工作原理","level":"2.7","depth":1,"next":{"title":"特殊指令ref的工作原理","level":"2.8","depth":1,"path":"1_online_rendering/4_directives/4_ref.md","ref":"1_online_rendering/4_directives/4_ref.md","articles":[]},"previous":{"title":"事件绑定v-on的工作原理","level":"2.6","depth":1,"path":"1_online_rendering/4_directives/2_event_binding.md","ref":"1_online_rendering/4_directives/2_event_binding.md","articles":[]},"dir":"ltr"},"config":{"plugins":["-lunr","-search","-sharing","theme-comscore","search-pro","code","back-to-top-button","tbfed-pagefooter","chapter-fold","expandable-chapters","splitter","anchor-navigation-ex","donate","sharing-plus","baidu"],"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"},"pluginsConfig":{"tbfed-pagefooter":{"copyright":"Copyright &copy fsjohnhuang 2022"},"chapter-fold":{},"splitter":{},"search-pro":{},"sharing-plus":{"qq":false,"all":["facebook","google","twitter","instapaper","linkedin","pocket","stumbleupon"],"douban":false,"facebook":true,"weibo":false,"instapaper":false,"whatsapp":false,"hatenaBookmark":false,"twitter":true,"messenger":false,"line":false,"vk":false,"pocket":true,"google":false,"viber":false,"stumbleupon":false,"qzone":false,"linkedin":false},"code":{"copyButtons":true},"donate":{"alipay":"","alipayText":"支付宝打赏","button":"赞赏","title":"","wechat":"/petite-vue-source-reading/images/wechat.jpg","wechatText":"微信打赏"},"fontsettings":{"theme":"white","family":"sans","size":2},"highlight":{},"anchor-navigation-ex":{"associatedWithSummary":true,"float":{"floatIcon":"fa fa-navicon","level1Icon":"","level2Icon":"","level3Icon":"","showLevelIcon":false},"mode":"float","multipleH1":true,"pageTop":{"level1Icon":"","level2Icon":"","level3Icon":"","showLevelIcon":false},"printLog":false,"showGoTop":false,"showLevel":true},"theme-comscore":{},"back-to-top-button":{},"baidu":{"token":"8bca1e3aa362a7f85dbe8304cff65225"},"sharing":{"qq":false,"all":["qq","qzone","weibo","whatsapp","twitter","facebook","line"],"douban":false,"facebook":false,"weibo":false,"instapaper":false,"whatsapp":false,"hatenaBookmark":false,"twitter":false,"messenger":false,"line":false,"vk":false,"pocket":false,"google":false,"viber":false,"stumbleupon":false,"qzone":false,"linkedin":false},"theme-default":{"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"},"showLevel":false},"expandable-chapters":{}},"theme":"default","author":"^_^肥仔John","lang":"zh-cn","pdf":{"pageNumbers":true,"fontSize":12,"fontFamily":"Arial","paperSize":"a4","chapterMark":"pagebreak","pageBreaksBefore":"/","margin":{"right":62,"left":62,"top":56,"bottom":56}},"structure":{"langs":"LANGS.md","readme":"README.md","glossary":"GLOSSARY.md","summary":"SUMMARY.md"},"isbn":"","variables":{},"title":"petite-vue源码剖析","gitbook":"*","description":""},"file":{"path":"1_online_rendering/4_directives/3_bidirectional_binding.md","mtime":"2022-04-26T01:46:57.470Z","type":"markdown"},"gitbook":{"version":"3.2.3","time":"2022-05-05T01:20:11.998Z"},"basePath":"../..","book":{"language":""}});
        });
    </script>
</div>

        
    <script src="../../gitbook/gitbook.js"></script>
    <script src="../../gitbook/theme.js"></script>
    
        
        <script src="../../gitbook/gitbook-plugin-search-pro/jquery.mark.min.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-search-pro/search.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-code/plugin.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-back-to-top-button/plugin.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-chapter-fold/chapter-fold.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-expandable-chapters/expandable-chapters.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-splitter/splitter.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-donate/plugin.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-sharing-plus/buttons.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-baidu/plugin.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-fontsettings/fontsettings.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-theme-comscore/test.js"></script>
        
    

    </body>
</html>

